package com.sfac.modules.account.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sfac.modules.account.domain.User;
import com.sfac.modules.account.service.UserService;
import com.sfac.modules.common.vo.Result;
import com.sfac.modules.common.vo.Search;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:唐琳的爹
 * @BelongsProject:hqyj_springboot
 * @BelongsPackage:com.sfac.modules.account.controller
 * @CreateTime:2022/8/27 14:25:15
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录方法
     * @param user
     * @return
     */
    @PostMapping(value = "/login")
    public Result<String> login(@RequestBody User user){

        return userService.login(user);
    }


    /**
     * 用户注册方法
     * @param user
     * @param
     * @return
     */
    @PostMapping(value = "/register",consumes = "application/json")
    public Result<String> register(@RequestBody User user){
        log.info("接收到{}",user);
        return userService.register(user);
    }

    /**
     * 分页查询所有用户方法
     * @param search
     * @return
     */
    @PostMapping(value = "/getUsers",consumes = "application/json")
    public Result<Page> getUserBySearchBean(@RequestBody Search search){
        return userService.getUserBySearchBean(search);
    }

    /**
     * 用于回显数据
     * @param id
     * @return
     */
    @GetMapping(value = "/user/{id}")
    public Result<User> getUser(@PathVariable String id){

        return userService.getUserById(id);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping(value = "/user/{id}")
    @RequiresPermissions("user/user/{id}")
    public Result<User> deleteUser(@PathVariable String id){
        return userService.deleteUserById(id);
    }


    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping(value = "/user",consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
}
